define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vppz/health_package/index' + location.search+'&ids='+Config.community_id,
                    add_url: 'vppz/health_package/add?ids='+Config.community_id,
                    edit_url: 'vppz/health_package/edit',
                    del_url: 'vppz/health_package/del',
                    table: 'vppz_health_package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
                        {field: 'name', title: '名称', operate: 'LIKE'},
                        {field: 'community.name', title: '所属社区', operate: 'LIKE'},
                        {field: 'show_price', title: '显示价格', operate: 'LIKE'},
                        {field: 'price', title: '实际价格', operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    var selectedServices = []; // 存储已选择的服务ID
// 初始化已选择的服务ID
    $('.service-item').each(function() {
        var serviceId = $(this).data('service-id');
        selectedServices.push(serviceId);
    });
    // 添加服务按钮点击事件
    $('#add-service-btn').click(function() {
        var $select = $('#service-select');
        var serviceId = $select.val();
        var serviceName = $select.find('option:selected').data('name');

        if (!serviceId) {
            Toastr.error('请选择服务');
            return;
        }

        // 检查是否已添加
        if (selectedServices.indexOf(serviceId) !== -1) {
            Toastr.error('该服务已添加，请勿重复选择');
            return;
        }

        // 添加到已选择列表
        selectedServices.push(serviceId);

        // 生成服务项HTML
        var html = '<div class="service-item" data-service-id="' + serviceId + '">' +
            '<i class="fa fa-times remove-service"></i>' +
            '<input type="hidden" name="row[service_id][]" value="' + serviceId + '">' +

            '<div class="form-group">' +
            '<label class="control-label">服务名称:</label>' +
            '<div class="form-value">' + serviceName + '</div>' +
            '</div>' +

            '<div class="form-group">' +
            '<label class="control-label">服务次数:</label>' +
            '<div class="form-value">' +
            '<input type="number" class="form-control" name="row[num][]" value="1" min="1" data-rule="required">' +
            '</div></div>';

            html += '<div class="form-group">' +
                '<label class="control-label">服务价值:</label>' +
                '<div class="form-value">' +
                '<input type="text" class="form-control" name="row[service_price][]" value="0" min="0" >' +
                '</div></div>';
            html += '<div class="form-group">' +
                '<label class="control-label">服务时长(分钟):</label>' +
                '<div class="form-value">' +
                '<input type="number" class="form-control" name="row[duration][]" value="0" min="0" >' +
                '</div></div>';

        html += '</div>';

        $('#services-container').append(html);

        // 重置选择框
        $select.val('').selectpicker('refresh');

        // 重新绑定表单验证
        Form.api.bindevent($("form[role=form]"));
    });

    // 删除服务事件(使用事件委托)
    $('#services-container').on('click', '.remove-service', function() {
        var $item = $(this).closest('.service-item');
        var serviceId = $item.data('service-id');

        // 从已选择列表中移除
        selectedServices = selectedServices.filter(function(id) {
            return id != serviceId;
        });

        $item.remove();
    });

    // 表单提交前处理
    // $("form[role=form]").submit(function() {
    //     if (selectedServices.length === 0) {
    //         Toastr.error('请至少添加一个服务');
    //         return false;
    //     }
    //     return true;
    // });
    return Controller;
});
